<template>
  <div class="container">
    <div class="app-container">
      <el-row type="flex" justify="end" style="padding: 10px">
        <el-button type="warning" @click="open">新增</el-button>
      </el-row>
      <el-table border :data="addressList">
        <el-table-column prop="id" align="center" label="序号" width="100px" />
        <el-table-column prop="receiver" align="center" label="收货人" />
        <el-table-column prop="mobile" align="center" label="手机号" />
        <el-table-column align="center" label="区域">
          <template #default="{row}">
            <span>{{ row.province + row.city + row.area }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="location" align="center" label="详细地址" />
        <el-table-column align="center" label="启用状态">
          <template #default="{row}">
            <el-switch v-model="row.state" :active-value="1" :inactive-value="0" />
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template #default="{row}">
            <el-button type="text" @click="edit(row.id)">修改</el-button>
            <el-button type="text" style="margin-left: 10px" @click="delClick(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <addressDialog
      ref="dialogRef"
      :show-dialog.sync="showDialog"
      @close="close"
      @update="getAddresses"
    />
  </div>
</template>
<script>
import { getAddressList, delAddress } from '@/api/address'
import addressDialog from '@/views/address/components/address-dialog.vue'
export default {
  name: 'Address',
  components: { addressDialog },
  data() {
    return {
      addressList: [],
      showDialog: false,
      // 读取数据的字段名
      props: { label: 'name' }
    }
  },
  created() {
    this.getAddresses()
  },
  methods: {
    // 关闭
    close() {
      this.showDialog = false
    },
    // 打开弹框
    open() {
      this.showDialog = true
    },
    // 修改
    edit(id) {
      this.showDialog = true
      this.$refs.dialogRef.getDetail(id)
    },
    // 展示数据
    async getAddresses() {
      const res = await getAddressList()
      this.addressList = res.data
    },
    // 删除数据
    delClick(id) {
      // 1. 询问用户
      this.$confirm('此操作将永久删除该权限点, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        // 2. 调用删除接口
        await delAddress(id)
        // 3. 提示用户
        this.$message.success('删除权限点成功')
        // 4. 刷新列表
        this.getAddresses()
      })
    }
  }
}
</script>

<style>

</style>
